﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Infrastructure
{
    /// <summary>
    /// DES加密/解密类
    /// </summary>
    public class DESEncrypt
    {
        public DESEncrypt()
        {
        }

        /// <summary>
        /// 长度为8
        /// </summary>
        public static string PublicKey = "12345678";

        /// <summary>   
        /// IV随便设置为8位   
        /// </summary>   
        private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return EncryptString(PublicKey, Text);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            return DecryptString(PublicKey, Text);
        }

        /// <summary>   
        /// 加密字符串   
        /// </summary>
        /// <returns>返回密文</returns>   
        public static String EncryptString(String key, String str)
        {
            byte[] data = Encoding.Default.GetBytes(str);
            byte[] result = EncryptData(key, data);
            if (result != null)
                return Convert.ToBase64String(result, 0, result.Length);
            else
                return "";
        }

        /// <summary>   
        /// 加密二进制数据   
        /// </summary>   
        /// <returns>返回二进制密文</returns>   
        public static byte[] EncryptData(String key, byte[] data)
        {
            byte[] bKey = null;
            if (key.Length > 0)
            {
                bKey = Encoding.Default.GetBytes(key.Substring(0, 8));
            }
            else
            {
                bKey = Encoding.Default.GetBytes(key);
            }
            byte[] bIV = IV;
            try
            {
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                desc.Mode = CipherMode.ECB;//兼容其他语言的Des加密算法   
                desc.Padding = PaddingMode.Zeros;//自动补0   
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
                cStream.Write(data, 0, data.Length);
                cStream.FlushFinalBlock();
                return mStream.ToArray();
            }
            catch
            {
                return null;
            }
        }

        /// <summary>   
        /// 把密文解密为明文   
        /// </summary>   
        /// <returns>返回明文</returns>   
        public static String DecryptString(String key, String decryptStr)
        {
            byte[] data = Convert.FromBase64String(decryptStr);
            byte[] result = DecryptData(key, data);
            if (result != null)
                return Encoding.Default.GetString(result, 0, result.Length).Replace("\0", "");
            else
                return "";
        }

        /// <summary>   
        /// 把二进制密文解密为明文二进制   
        /// </summary>   
        /// <returns>返回明文二进制</returns>   
        public static byte[] DecryptData(String key, byte[] data)
        {
            try
            {
                byte[] bKey = Encoding.Default.GetBytes(key.Substring(0, 8));
                byte[] bIV = IV;
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                desc.Mode = CipherMode.ECB;//兼容其他语言的Des加密算法   
                desc.Padding = PaddingMode.Zeros;//自动补0   
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, desc.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write);
                cStream.Write(data, 0, data.Length);
                cStream.FlushFinalBlock();
                return mStream.ToArray();
            }
            catch
            {
                return null;
            }
        }
    }
}